//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.util.JdbcUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.yaukie.base.config.properties.DruidDataSourceProperties;
import org.yaukie.base.constant.SysConstant.DsType;
import org.yaukie.base.datasource.DynamicDataSource;
import org.yaukie.base.util.SpringContextUtil;
import org.yaukie.base.util.StringTools;

@Configuration
public class DruidConfig {
    private static final Logger log = LoggerFactory.getLogger(DruidConfig.class);

    public DruidConfig() {
    }

    @Bean({"master"})
    @ConfigurationProperties("spring.datasource.druid.master")
    public DruidDataSource setMain(DruidDataSourceProperties properties) {
        DruidDataSource druidDataSource = null;

        try {
            druidDataSource = DruidDataSourceBuilder.create().build();
            properties.dataSource(druidDataSource);
            if (log.isInfoEnabled()) {
                log.info("===主数据源初始化成功,url--{}", druidDataSource.getRawJdbcUrl());
            }
        } catch (Exception var4) {
            if (log.isInfoEnabled()) {
                log.info("===主数据源初始化异常,url--{}", druidDataSource.getRawJdbcUrl());
            }
        }

        return druidDataSource;
    }

    @Bean({"slave"})
    @ConditionalOnProperty(
            prefix = "spring.datasource.druid.slave",
            name = {"enabled"},
            havingValue = "true"
    )
    @ConfigurationProperties("spring.datasource.druid.slave")
    public DruidDataSource setSecond(DruidDataSourceProperties properties) {
        DruidDataSource druidDataSource = null;

        try {
            druidDataSource = DruidDataSourceBuilder.create().build();
            properties.dataSource(druidDataSource);
            if (log.isInfoEnabled()) {
                log.info("===从数据源初始化成功,url--{}", druidDataSource.getRawJdbcUrl());
            }
        } catch (Exception var4) {
            if (log.isErrorEnabled()) {
                log.error("===从数据源初始化异常,url--{}", druidDataSource.getRawJdbcUrl());
            }
        }

        return druidDataSource;
    }

    @Bean({"dynamicDataSource"})
    @Primary
    public DynamicDataSource setDynamicDataSource(DataSource master) {
        Map<Object, Object> targetDataSources = new HashMap();
        DynamicDataSource dynamicDataSource = new DynamicDataSource();

        try {
            DataSource slave = SpringContextUtil.containsBean("slave") ? (DataSource)SpringContextUtil.getBean("slave") : null;
            dynamicDataSource.setDefaultTargetDataSource(master);
            targetDataSources.put(DsType.MASTER.name(), master);
            if (StringTools.isNotNull(slave)) {
                targetDataSources.put(DsType.SLAVE.name(), slave);
            }

            dynamicDataSource.setTargetDataSources(targetDataSources);
            dynamicDataSource.afterPropertiesSet();
        } catch (Exception var5) {
            if (log.isErrorEnabled()) {
                log.error("数据源初始化异常{}", var5);
            }
        }

        return dynamicDataSource;
    }

  /*  @Bean
    @ConditionalOnProperty(
            prefix = "spring.datasource.druid.statViewServlet",
            name = {"enabled"},
            havingValue = "true"
    )
    public FilterRegistrationBean filterRegistrationBean(DruidStatProperties druidStatProperties) {
        DruidStatProperties.StatViewServlet statViewServlet = druidStatProperties.getStatViewServlet();
        final String commonJsPath = "support/http/resources/js/common.js";
        String druidServletPath = statViewServlet.getUrlPattern() == null ? "/drd/*" : statViewServlet.getUrlPattern();
        String fileRequestPath = druidServletPath.replaceAll("\\*", "js/common.js");
        Filter filter = new Filter() {
            public void init(FilterConfig filterConfig) throws ServletException {
            }

            public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
                filterChain.doFilter(request, response);
                response.resetBuffer();
                String text = DruidConfig.this.readFileResource(commonJsPath);
                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
                text = text.replaceAll("powered.*?shrek.wang</a>", "");
                response.getWriter().print(text);
            }

            public void destroy() {
            }
        };
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.addUrlPatterns(new String[]{fileRequestPath});
        return filterRegistrationBean;
    }

    private String readFileResource(String resource) {
        if (resource != null && !resource.isEmpty() && !resource.contains("..") && !resource.contains("?") && !resource.contains(":")) {
            InputStream in = null;

            InputStreamReader isr;
            try {
                in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
                if (in == null) {
                    in = DruidConfig.class.getResourceAsStream(resource);
                }

                String text = null;
                if (in != null) {
                    isr = null;
                    isr = new InputStreamReader(in, "UTF-8");
                    char[] buffer = new char[4096];
                    StringWriter sw = new StringWriter();

                    int len;
                    while((len = isr.read(buffer)) != -1) {
                        sw.write(buffer, 0, len);
                    }

                    sw.flush();
                    String var8 = sw.toString();
                    return var8;
                }

                isr = new InputStreamReader(text);
            } catch (UnsupportedEncodingException var13) {
                throw new IllegalStateException(var13.getMessage(), var13);
            } catch (IOException var14) {
                throw new IllegalStateException(var14.getMessage(), var14);
            } finally {
                JdbcUtils.close(in);
            }

            return isr;
        } else {
            return "";
        }
    }*/
}
